package leetcode.median;

import leetcode.base.ListNode;

public class $86_Partition {
    public ListNode partition(ListNode head, int x) {
        ListNode dummy1 = new ListNode(-1);
        ListNode dummy2 = new ListNode(-1);
        ListNode p1 = dummy1;
        ListNode p2 = dummy2;

        ListNode cur = head;
        while (cur != null) {
            if (cur.val < x) {
                p1.next = cur;
                p1 = p1.next;
            } else {
                p2.next = cur;
                p2 = p2.next;
            }

            ListNode temp = cur.next;
            cur.next = null;
            cur = temp;
        }
        p1.next = dummy2.next;
        return dummy1.next;
    }
}
